<template>
  <div id="container">
    <el-form
      :model="form"
      :rules="rules"
      :status-icon="true"
      ref="form"
      label-width="100px"
      class="demo-form"
    >
      <el-form-item label="考题类型" prop="type">
        <el-select v-model="form.type">
          <el-option label="单选题" value="1"></el-option>
          <el-option label="多选题" value="2"></el-option>
          <el-option label="判断题" value="3"></el-option>
          <el-option label="填空题" value="4"></el-option>
          <el-option label="简答题" value="5"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="考题内容" prop="content">
        <el-input v-model="form.content"></el-input>
      </el-form-item>
      <el-form-item label="正确答案" prop="rightAnswersContent">
        <template v-for="(item, index) of form.rightAnswers">
          <el-input
            v-model="form.rightAnswers[index]"
            v-bind:key="index"
            style="margin-bottom: 20px"
          ></el-input>
        </template>
      </el-form-item>
      <el-form-item label="" prop="">
        <el-button
          type="primary"
          icon="el-icon-plus"
          @click="jumpToAdd()"
        ></el-button>
      </el-form-item>
      <el-form-item label="错误答案" prop="name">
        <template v-for="(item, index) in form.wrongAnswers">
          <el-input
            v-model="form.wrongAnswers[index]"
            v-bind:key="index"
            style="margin-bottom: 20px"
          ></el-input>
        </template>
        <el-button
          type="primary"
          icon="el-icon-plus"
          @click="jumpToAdd()"
        ></el-button>
      </el-form-item>
      <el-form-item label="解析" prop="analysis">
        <el-input type="textarea" v-model="form.analysis"></el-input>
      </el-form-item>
      <!-- <el-form-item label="活动时间" required>
        <el-col :span="11">
          <el-form-item prop="date1">
            <el-date-picker
              type="date"
              placeholder="选择日期"
              v-model="form.date1"
              style="width: 100%"
            ></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col class="line" :span="2">-</el-col>
        <el-col :span="11">
          <el-form-item prop="date2">
            <el-time-picker
              placeholder="选择时间"
              v-model="form.date2"
              style="width: 100%"
            ></el-time-picker>
          </el-form-item>
        </el-col>
      </el-form-item>
      <el-form-item label="即时配送" prop="delivery">
        <el-switch v-model="form.delivery"></el-switch>
      </el-form-item>
      <el-form-item label="活动性质" prop="type">
        <el-checkbox-group v-model="form.type">
          <el-checkbox label="美食/餐厅线上活动" name="type"></el-checkbox>
          <el-checkbox label="地推活动" name="type"></el-checkbox>
          <el-checkbox label="线下主题活动" name="type"></el-checkbox>
          <el-checkbox label="单纯品牌曝光" name="type"></el-checkbox>
        </el-checkbox-group>
      </el-form-item>
      <el-form-item label="特殊资源" prop="resource">
        <el-radio-group v-model="form.resource">
          <el-radio label="线上品牌商赞助"></el-radio>
          <el-radio label="线下场地免费"></el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="活动形式" prop="desc">
        <el-input type="textarea" v-model="form.desc"></el-input>
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" @click="submitForm('form')"
          >立即创建</el-button
        >
        <el-button @click="resetForm('form')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import request from "@/utils/request";
import { showLoading } from "@/utils/util";
export default {
  name: "QuestionAdd",
  data() {
    let validateRightAnswersContent = (rule, value, callback) => {
      console.log(this.form.rightAnswers);
      const rightAnswers = this.form.rightAnswers;
      let check = false;
      for (const answer of rightAnswers) {
        if (answer) {
          check = true;
          break;
        }
      }
      if (check) {
        callback();
      } else {
        callback(new Error("请至少填写一个正确答案"));
      }
    };
    return {
      form: {
        content: "",
        rightAnswers: [""],
        wrongAnswers: ["", "", ""],
        date1: "",
        date2: "",
        delivery: false,
        type: "1",
        resource: "",
        desc: "",
      },
      rules: {
        content: [
          { required: true, message: "请输入考题类型", trigger: "blur" },
        ],
        type: [
          { required: true, message: "请选择考题类型", trigger: "change" },
        ],
        rightAnswersContent: [
          {
            required: true,
            validator: validateRightAnswersContent,
            trigger: "blur",
          },
        ],
        content: [
          { required: true, message: "请输入考题类型", trigger: "blur" },
        ],
        date1: [
          {
            type: "date",
            required: true,
            message: "请选择日期",
            trigger: "change",
          },
        ],
        date2: [
          {
            type: "date",
            required: true,
            message: "请选择时间",
            trigger: "change",
          },
        ],
        // type: [
        //   {
        //     type: "array",
        //     required: true,
        //     message: "请至少选择一个活动性质",
        //     trigger: "change",
        //   },
        // ],
        resource: [
          { required: true, message: "请选择活动资源", trigger: "change" },
        ],
        desc: [{ required: true, message: "请填写活动形式", trigger: "blur" }],
      },
    };
  },
  watch: {
    // 如果 `form.type` 发生改变，这个函数就会运行
    "form.type": function (newQuestion, oldQuestion) {
      // alert(newQuestion)
    },
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let params = Object.assign({}, this.form);
          params.rightAnswersContent = params.rightAnswers.join("|")
          params.wrongAnswersContent = params.wrongAnswers.join("|")
          delete params.rightAnswers
          delete params.wrongAnswers
          console.log(params);
          request({
            url: '/api/question',
            method: 'post',
            params,
          }).then(res => {
            console.log(res)
          })
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>
<style lang="scss">
#container {
  width: 900px;
  height: 85%;
  padding: 30px;
  background-color: #fff;
  border-radius: 10px;

  .line {
    text-align: center;
  }
}
</style>